/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author mwh
 */
public class FertilityTableMem implements FertilityTable {

    final Map<Integer, List<Double>> table;


    public Iterator<Integer> keyIterator() {
        return table.keySet().iterator();
    }


    public FertilityTableMem(String filename) throws FileNotFoundException, IOException {

        table = new HashMap<Integer, List<Double>>();

        BufferedReader input = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = input.readLine()) != null) {

            String[] tokens = line.split(" ");

            Integer key = Integer.parseInt(tokens[0]);

            List<Double> fertList = new ArrayList<Double>(tokens.length - 1);
            for (int i = 1; i < tokens.length; i++) {
                fertList.add(Double.parseDouble(tokens[i]));
            }

            table.put(key, fertList);
        }
    }
//
//
//    public List<Double> get(int word) {
//        if (table.containsKey(word)) {
//            return table.get(word);
//        }
//        return new ArrayList<Double>();
//    }
    public List<Double> get(int word) {
        if (table.containsKey(word)) {
            return table.get(word);
        }
        return new ArrayList<Double>();
    }


    public double get(int word, int fert) {

        List<Double> ferts = get(word);

        if (ferts.size() > fert) {
            return ferts.get(fert);
        }
        return Double.NaN;

    }


    public double getLog10(int word, int fert) {
        return Math.log10(get(word, fert));
    }
}
